package net.stogi.dsper.ui.server;

import net.stogi.common.gwt.dispatch.server.core.ActionHandler;
import net.stogi.common.gwt.dispatch.shared.exception.ActionException;
import net.stogi.common.security.exception.AuthenticationException;
import net.stogi.common.security.service.SecurityService;
import net.stogi.dsper.ui.shared.GetPrincipalDataAction;
import net.stogi.dsper.ui.shared.GetPrincipalDataResult;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("DsperUi.Dispatch.GetPrincipalDataActionHandler")
public class GetPrincipalDataActionHandler implements ActionHandler<GetPrincipalDataAction, GetPrincipalDataResult> {

	private static final Logger log = LoggerFactory.getLogger(GetPrincipalDataActionHandler.class);

	@Autowired
	private SecurityService securityService;

	@Override
	public GetPrincipalDataResult execute(GetPrincipalDataAction action) throws ActionException {
		log.trace("GetPrincipalDataActionHandler: [ action = {}]", action);

		GetPrincipalDataResult result;

		try {
			result = new GetPrincipalDataResult(securityService.getLoggedUserPrincipalData());
		}
		catch (AuthenticationException e) {
			throw new ActionException(e);
		}

		return result;
	}

	@Override
	public Class<GetPrincipalDataAction> getActionType() {
		return GetPrincipalDataAction.class;
	}

	@Override
	public void rollback(GetPrincipalDataAction action, GetPrincipalDataResult result) throws ActionException {}
}
